package com.maybach.util;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by houenxun on 16/1/14.
 * 图片链接工具 对多个图片链接进行统一拆分和合并
 */
public class ImgUrlUtil {
    public static final String SPLIT = "#*#";
    public static List<String> parseList(String imgs){
        List<String> list = new ArrayList<String>();
        if(StringUtils.isBlank(imgs)){
            return list;
        }
        String [] imgArray = imgs.split(SPLIT);
        for(String img: imgArray){
            if(StringUtils.isNotBlank(img)&&!img.equals("*")){
                list.add(img.trim());
            }
        }
        return list;
    }

    public static String mergeList(List<String> list){
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        StringBuilder builder = new StringBuilder();
        for(String img: list){
            if(StringUtils.isNotBlank(img)){
                builder.append(img.trim()).append(SPLIT);
            }
        }
        return builder.substring(0, builder.length() - SPLIT.length());
    }

}
